package com.qingrongby.shop.util;

import com.alibaba.fastjson.JSON;
import org.apache.commons.lang.StringUtils;

import javax.persistence.AttributeConverter;
import javax.persistence.Converter;
import java.util.HashMap;
import java.util.Map;

/**
 * @CLASSNAME CovertyArrayAndJson
 * @DESCRIPTION TODO
 * @AUTHOR hzp
 * @DATE 2020/6/29
 * @VERSION 1.0
 **/
@Converter
public class ConverterArrayAndJson implements AttributeConverter<Map<String,Object>,String> {

    @Override
    public String convertToDatabaseColumn(Map<String, Object> stringObjectMap) {
        if(stringObjectMap.isEmpty()){
            return "";
        }
        String jsonString = JSON.toJSONString(stringObjectMap);
        return jsonString;
    }

    @Override
    public Map<String, Object> convertToEntityAttribute(String s) {
        if(StringUtils.isBlank(s)){
            return null;
        }
        Map<String,Object> map= JSON.parseObject(s,HashMap.class);
        return map;
    }
}
